/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package cn.ac.iie.di.cst.lls.util;

import cn.ac.iie.di.cst.lls.common.RuntimeEnv;
import cn.ac.iie.di.cst.lls.se.CstdxStartup;
import cn.ac.iie.di.cst.lls.worker.RocketdxConsumer;
import java.util.Map;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.LinkedBlockingQueue;
import org.apache.avro.generic.GenericRecord;
import org.apache.log4j.Logger;
import sun.misc.Signal;
import sun.misc.SignalHandler;

/**
 *
 * @author alexmu
 */
public class KillHandler implements SignalHandler {

    public static Logger log = Logger.getLogger(KillHandler.class.getName());

    public KillHandler() {
    }

    public void registerSignal(String signalName) {
        Signal signal = new Signal(signalName);
        Signal.handle(signal, this);
    }

    @Override
    public void handle(Signal signal) {
        log.info("now receive the system signal " + signal.getName() + " " + signal.getNumber() + "\n\n");
        if (signal.getName().equals("TERM")) {
//            SE2DWStartup.stop();
            RocketdxConsumer.stop();
            int count = 0;
            while (true) {
                try {
                    Thread.sleep(5L);
                } catch (Exception ex) {
                }
                count++;
                for (Map.Entry<String, BlockingQueue> entry : RuntimeEnv.mq2queue.entrySet()) {
                    if (!entry.getValue().isEmpty()) {
                        log.info("the "+entry.getKey()+"  queue size is "+entry.getValue().size());
                        count = 0;
                    }
                }
                log.info("the comment queue size is "+CstdxStartup.ab.size());
                if (count >= 20&&CstdxStartup.ab.isEmpty()) {
                    log.info("now the system will exit safely .... \n\n");
                    System.exit(0);
                }
            }
        } else if (signal.getName().equals("INT") || signal.getName().equals("HUP")) {
//            SE2DWStartup.stop();
            RocketdxConsumer.stop();
            int count = 0;
            while (true) {
                try {
                    Thread.sleep(5L);
                } catch (Exception ex) {
                }
                count++;
                for (Map.Entry<String, BlockingQueue> entry : RuntimeEnv.mq2queue.entrySet()) {
                    if (!entry.getValue().isEmpty()) {
                         log.info("the "+entry+"  queue size is "+entry.getValue().size());
                        count = 0;
                    }
                }
                log.info("the comment queue size is "+CstdxStartup.ab.size());
                if (count >= 2000&&CstdxStartup.ab.isEmpty()) {
                    log.info("now the system will exit safely .... \n\n");
                    System.exit(0);
                }
            }
        } else {
            log.info("can not process the system signal " + signal.toString());
        }
    }
}
